package com.fau.hm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.hm.action.PublicAction;
import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalLeaveInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 休假管理-请假管理
 * @author dangjh
 * @datetime 2014年10月26日,上午11:30:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class VacationAction extends PublicAction implements ModelDriven<MedicalLeaveInfo> {

	private static final long serialVersionUID = 1713700405822813176L;

	private MedicalLeaveInfo leaveInfo = new MedicalLeaveInfo();
	
	/**
	 * @desc 实例化请假管理界面
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年10月27日,下午4:41:28
	 */
	@Action(value = "init_leave_management", results = {@Result(location = "init_leave_management.jsp")})
	public String initLeaveManagement(){
		return SUCCESS;
	}
	
	/**
	 * @desc 实例化休假记录界面（全公司）
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年10月27日,下午4:41:28
	 */
	@Action(value = "init_leave_management_log", results = {@Result(location = "init_leave_management_log.jsp")})
	public String initLeaveManagementLog(){
		return SUCCESS;
	}
	
	/**
	 * @desc 实例化休假记录列表页
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年10月25日,下午3:44:28
	 */
	@Action(value = "init_leave_info_list", results = {@Result(location = "init_leave_info_list.jsp")})
	public String initLeaveInfoList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 休假记录列表数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月25日,下午3:48:09
	 */
	@Action(value = "leave_info_view")
	public String leaveInfoView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getLeaveService().findLeaveInfo(map, leaveInfo, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 实例化批假信息页面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月27日,下午11:43:52
	 */
	@Action(value = "init_approve_leave_list", results = {@Result(location = "init_approve_leave_list.jsp")})
	public String approveLeaveList(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载本企业所有带审核请假记录信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月27日,下午11:44:36
	 */
	@Action(value = "approve_leave_view")
	public String approveLeaveView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		this.pageBean = this.baseAllService.getLeaveService().findLeaveApproveInfo(map, leaveInfo, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 新建休假信息界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月25日,下午4:43:17
	 */
	@Action(value = "leave_info_add", results = {@Result(location = "leave_info_add.jsp")})
	public String leaveInfoAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存休假信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月13日,下午1:53:30
	 */
	@Action(value = "leave_info_save")
	public String leaveInfoSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		boolean flag = this.baseAllService.getLeaveService().saveLeaveInfo(leaveInfo, admin);
		if(flag){
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 编辑已回收的休假信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月4日,下午11:54:13
	 */
	@Action(value = "medical_backleave_edit", results = {@Result(location = "medical_backleave_edit.jsp")})
	public String backLeaveEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 编辑回收后的休假信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月13日,下午1:53:30
	 */
	@Action(value = "leave_backedit_save")
	public String leaveBackEdidSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(leaveInfo.getId())){
			boolean flag = this.baseAllService.getLeaveService().editLeaveInfo(leaveInfo);
			if(flag){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除休假信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月13日,下午4:03:19
	 */
	@Action(value = "leave_info_del")
	public String leaveInfoDel(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(leaveInfo.getId())){
			this.baseAllService.getLeaveService().deleteLeaveInfo(leaveInfo);
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查看详细
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月25日,下午5:37:37
	 */
	@Action(value = "init_leave_details", results = {@Result(location = "init_leave_details.jsp")})
	public String leaveDetails(){
		return SUCCESS;
	}
	
	/**
	 * @desc 收回操作-休假状态修改
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月23日,上午10:54:23
	 */
	@Action(value = "leave_status_back")
	public String leaveStatusBack(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(leaveInfo.getId())){
			boolean flag = this.baseAllService.getLeaveService().editLeaveBack(leaveInfo);
			if(flag){
				map.put("success", true);
			}else{
				map.put("success", false);
			}
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalLeaveInfo getModel() {
		return leaveInfo;
	}

}
